package com.tzinfo.tzt.activity.util;

import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.tzinfo.tzt.activity.MapRoutePlanActivity;
import com.tzinfo.tzt.config.FilterConfig;

/**
 * Created by bethinner on 12/30/15.
 */
public class ConvertSwitch {
    public static   double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    /// <summary>
/// 中国正常坐标系GCJ02协议的坐标，转到 百度地图对应的 BD09 协议坐标
/// </summary>
/// <param name="lat">维度</param>
/// <param name="lng">经度</param>
    public static void Convert_GCJ02_To_BD09( double lat, double lng)
    {
        double x = lng, y = lat;
        double z =Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        lng = z * Math.cos(theta) + 0.0065;
        lat = z * Math.sin(theta) + 0.006;
        MapRoutePlanActivity.locationLat=lat;
        MapRoutePlanActivity.locationLon=lng;
    }

    public static GeoPoint Convert_GCJ02_To_BD09_GeoPoint( double lat, double lng)
    {
        double x = lng, y = lat;
        double z =Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        lng = z * Math.cos(theta) + 0.0065;
        lat = z * Math.sin(theta) + 0.006;
        return new GeoPoint((int) (lat * 1E6),
                (int) (lng * 1E6));
    }

    /// <summary>
/// 百度地图对应的 BD09 协议坐标，转到 中国正常坐标系GCJ02协议的坐标
/// </summary>
/// <param name="lat">维度</param>
/// <param name="lng">经度</param>
    public static void Convert_BD09_To_GCJ02( double lat,  double lng)
    {
        double x = lng - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        lng = z * Math.cos(theta);
        lat = z * Math.sin(theta);
    }
    public static String ConvertBD09ToGCJ02String( double lat,  double lng)
    {
        double x = lng - 0.0065, y = lat - 0.006;
//        double x = 120.103892 - 0.0065, y = 30.271953 - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        lng = z * Math.cos(theta);
        lat = z * Math.sin(theta);
        return "&lon="+ lng + "&lat=" + lat;
    }
}
